package com.meitianhui.convenitenbuy.module.order.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseActivity;
import com.meitianhui.convenitenbuy.hyphenate.ui.ChatActivity;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.BrandOrderFragment;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.EdinburghOrderFragment;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.FightGroupOrderFragment;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.HomeServiceOrderFragment;
import com.meitianhui.convenitenbuy.util.RxUtils;

/**
 * Created by Administrator on 2017/11/28.
 */

public class OrderActivity extends BaseActivity {
    public Fragment orderFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    //type1 宅急送（便利店购买） type2 伙拼团 type3 超级返（领样） type4 金币兑（兑换）
    private int mOrderType = 0;
    private ImageView mTbBackAll;
    private TextView mTb_title;
    private ImageView mToolbarIvMenu;
    private String s_hand = "https://oss-img.meitianhui.com/communityDocs/kefu.jpg";
    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_order);
        mTbBackAll = getViewById(R.id.toolbar_back_all);
        RxUtils.bindEvents(mTbBackAll, this);
        mTb_title = getViewById(R.id.toolbar_title);
        mToolbarIvMenu = getViewById(R.id.toolbar_iv_menu);
        RxUtils.bindEvents(mToolbarIvMenu, this);
        RxView.visibility(mToolbarIvMenu).call(true);
        mToolbarIvMenu.setBackgroundResource(R.mipmap.icon_title_message);
        initData();
    }

    private void initData() {
        mOrderType = getIntent().getIntExtra("order_type", 0);
        if (mOrderType == 1) {
            mTb_title.setText("宅急送");
        } else if (mOrderType == 2) {
            mTb_title.setText("拼团领");
        } else if (mOrderType == 3) {
            mTb_title.setText("淘淘领");
        } else if (mOrderType == 4) {
            mTb_title.setText("品牌领");
        }
        initFragmentManager();
    }


    private void initFragmentManager() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        if (mOrderType == 1) {
            orderFragment = new HomeServiceOrderFragment();
            transaction.replace(R.id.content, orderFragment, HomeServiceOrderFragment.Tag);
        } else if (mOrderType == 2) {
            orderFragment = new FightGroupOrderFragment();
            transaction.replace(R.id.content, orderFragment, FightGroupOrderFragment.Tag);

        } else if (mOrderType == 3) {
            orderFragment = new EdinburghOrderFragment();
            transaction.replace(R.id.content, orderFragment, EdinburghOrderFragment.Tag);
        } else if (mOrderType == 4) {
            orderFragment = new BrandOrderFragment();
            transaction.replace(R.id.content, orderFragment, BrandOrderFragment.Tag);
        }
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.toolbar_back_all:  //返回
                this.finish();
                break;
            case R.id.toolbar_iv_menu:
                clickMenu();
                break;
        }
    }

    protected void clickMenu() {
        String chatId = "";
        String title = "官方客服";
        if (mOrderType == 1) {
            //宅急送
            chatId = Constants.HYPHENATE_ID_HOME_SERVICE;
        } else if (mOrderType == 2) {
            //伙拼团
            chatId = Constants.HYPHENATE_ID_FIGHT_GROUP;
        } else if (mOrderType == 3) {
            //领了么
            chatId = Constants.HYPHENATE_ID_EDINBURGH;
        } else if (mOrderType == 4) {
            //名品汇
            chatId = Constants.HYPHENATE_ID_BRAND;
        }
        Intent intent = new Intent(OrderActivity.this, ChatActivity.class);
        intent.putExtra("userId", chatId);
        intent.putExtra("groupName", title);
        startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
